package com.example.shiyan01

import android.content.Context
import android.content.res.Configuration
import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.Locale

class MainActivity : AppCompatActivity() {

    private lateinit var helloText: TextView
    private lateinit var welcomeText: TextView
    private lateinit var flagImageView: ImageView
    private lateinit var currentLanguageText: TextView


    private val languages = listOf("en", "zh", "es")
    private val languageNames = listOf("English", "中文", "Español")


    private val flagResources = mapOf(
        "en" to R.drawable.flag_english,
        "zh" to R.drawable.flag_chinese,
        "es" to R.drawable.flag_spanish
    )


    private val languageColors = mapOf(
        "en" to Color.BLUE,
        "zh" to Color.RED,
        "es" to Color.YELLOW
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        applySavedLanguage()
        createUI()

        updateFlagDisplay()
    }

    private fun createUI() {

        val mainLayout = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            setBackgroundColor(Color.WHITE)
            setPadding(50, 100, 50, 50)
        }


        val titleText = TextView(this).apply {
            text = "多语言 Hello World"
            textSize = 20f
            setTextColor(Color.BLACK)
            setPadding(0, 0, 0, 30)
        }


        val flagContainer = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            setPadding(0, 0, 0, 20)
        }


        flagImageView = ImageView(this).apply {
            layoutParams = LinearLayout.LayoutParams(200, 120).apply {
                gravity = android.view.Gravity.CENTER
            }
            scaleType = ImageView.ScaleType.FIT_CENTER
        }


        currentLanguageText = TextView(this).apply {
            textSize = 16f
            setTextColor(Color.DKGRAY)
            setPadding(0, 10, 0, 10)
            gravity = android.view.Gravity.CENTER
        }

        flagContainer.addView(flagImageView)
        flagContainer.addView(currentLanguageText)


        welcomeText = TextView(this).apply {
            text = getString(R.string.welcome_message)
            textSize = 16f
            setTextColor(Color.DKGRAY)
            setPadding(0, 0, 0, 20)
            gravity = android.view.Gravity.CENTER
        }


        helloText = TextView(this).apply {
            text = getString(R.string.hello_world)
            textSize = 24f
            setTextColor(Color.BLUE)
            setPadding(0, 0, 0, 40)
            gravity = android.view.Gravity.CENTER
        }


        val languageLayout = LinearLayout(this).apply {
            orientation = LinearLayout.VERTICAL
            setPadding(0, 0, 0, 50)
        }


        createLanguageButtons(languageLayout)


        val touchButton = Button(this).apply {
            text = "点击我测试交互!"
            setBackgroundColor(Color.GREEN)
            setTextColor(Color.WHITE)
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            setOnClickListener {
                helloText.text = "按钮被点击了! 👆\n${getString(R.string.hello_world)}"
                helloText.setTextColor(Color.RED)
            }
        }


        mainLayout.addView(titleText)
        mainLayout.addView(flagContainer)
        mainLayout.addView(welcomeText)
        mainLayout.addView(helloText)
        mainLayout.addView(languageLayout)
        mainLayout.addView(touchButton)


        setContentView(mainLayout)
    }

    private fun createLanguageButtons(container: LinearLayout) {
        for (i in languages.indices) {
            val button = Button(this).apply {
                text = "切换到 ${languageNames[i]}"
                setBackgroundColor(languageColors[languages[i]] ?: Color.GRAY)
                setTextColor(Color.WHITE)
                layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                ).apply {
                    setMargins(0, 10, 0, 10)
                }


                setOnClickListener {
                    changeLanguage(languages[i])
                }
            }

            container.addView(button)
        }
    }

    private fun changeLanguage(languageCode: String) {

        saveLanguagePreference(languageCode)


        applyLanguage(languageCode)


        updateUI()


        updateFlagDisplay()
    }

    private fun updateUI() {

        welcomeText.text = getString(R.string.welcome_message)
        helloText.text = getString(R.string.hello_world)
        helloText.setTextColor(Color.BLUE)
    }

    private fun updateFlagDisplay() {
        val currentLang = getSavedLanguage()
        val flagRes = flagResources[currentLang] ?: R.drawable.flag_english


        flagImageView.setImageResource(flagRes)


        currentLanguageText.text = "当前语言: ${getCurrentLanguageName()}"
    }

    private fun getCurrentLanguageName(): String {
        val currentLang = getSavedLanguage()
        return when (currentLang) {
            "en" -> "English"
            "zh" -> "中文"
            "es" -> "Español"
            else -> "System Default"
        }
    }

    private fun applyLanguage(languageCode: String) {
        val locale = when (languageCode) {
            "en" -> Locale.ENGLISH
            "zh" -> Locale.SIMPLIFIED_CHINESE
            "es" -> Locale("es")
            else -> Locale.getDefault()
        }

        Locale.setDefault(locale)

        val resources = resources
        val configuration = Configuration(resources.configuration)
        configuration.setLocale(locale)


        resources.updateConfiguration(configuration, resources.displayMetrics)


        createConfigurationContext(configuration)
    }

    private fun applySavedLanguage() {
        val savedLanguage = getSavedLanguage()
        if (savedLanguage.isNotEmpty()) {
            applyLanguage(savedLanguage)
        }
    }

    private fun saveLanguagePreference(languageCode: String) {
        val sharedPreferences = getSharedPreferences("AppPreferences", Context.MODE_PRIVATE)
        sharedPreferences.edit().putString("selected_language", languageCode).apply()
    }

    private fun getSavedLanguage(): String {
        val sharedPreferences = getSharedPreferences("AppPreferences", Context.MODE_PRIVATE)
        return sharedPreferences.getString("selected_language", "en") ?: "en"
    }
}